﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using Newtonsoft.Json;
using System.Configuration;
using System.Data;

namespace PromotionEngine
{
    public partial class Doc_Property_Link : System.Web.UI.Page
    {
        public class Response
        {
            public List<Model.DocPropertyLink> Data { get; set; }
            public int Count { get; set; }
            public string Errors { get; set; }

            public Response(List<Model.DocPropertyLink> data, int count)
            {
                this.Data = data;
                this.Count = count;
            }

            public Response(string errors)
            {
                this.Errors = errors;
            }

            public Response()
            {

            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string docEntry = Request.QueryString["DocEntry"];
            this.lblDocEntry.Text = docEntry;
            string docStatus = Request.QueryString["DocStatus"];
            this.hdnDocStatus.Value = docStatus;
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetDocPropertyLinks()
        {
            List<Model.DocPropertyLink> lstDocProperty = new List<Model.DocPropertyLink>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocPropertyLink(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), HttpContext.Current.Session["CompanyCode"].ToString());
            if (dsPromotion != null)
            {

                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocPropertyLink docPropertyLink = new Model.DocPropertyLink();
                    docPropertyLink.PropertyLinkID = r["PropertyLinkID"].ToString();
                    docPropertyLink.DocEntry = r["DocEntry"].ToString();
                    docPropertyLink.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docPropertyLink.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docPropertyLink.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstDocProperty.Add(docPropertyLink);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocProperty };
                return new Response(lstDocProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocPropertyLink(string oDocPropertyLink, bool isInsert)
        {
            Model.DocPropertyLink itemPropertyLink = JsonConvert.DeserializeObject<Model.DocPropertyLink>(oDocPropertyLink);

            Promotion pmt = new Promotion();
            string errMsg = pmt.UpdateJsonDocPropertyLink(itemPropertyLink.DocEntry, itemPropertyLink.PropertyDetailID, itemPropertyLink.PropertyLinkID,
                  ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
            if (errMsg.Length == 0)
            {
                //Show error message
            }
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static Response GetDocPropertyLinkByDoc(string docEntry, string propertyID)
        {
            List<Model.DocPropertyLink> lstDocProperty = new List<Model.DocPropertyLink>();
            Promotion pmt = new Promotion();
            DataSet dsPromotion = pmt.GetAllDocPropertyLinkByItem(ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), propertyID, docEntry);
            if (dsPromotion != null)
            {
                foreach (DataRow r in dsPromotion.Tables[0].Rows)
                {
                    Model.DocPropertyLink docProperty = new Model.DocPropertyLink();
                    docProperty.PropertyLinkID = r["PropertyLinkID"].ToString();
                    docProperty.DocEntry = r["DocEntry"].ToString();
                    docProperty.PropertyDetailID = r["PropertyDetailID"].ToString();
                    docProperty.PropertyDetailCode = r["PropertyDetailCode"].ToString();
                    docProperty.PropertyDetailName = r["PropertyDetailName"].ToString();
                    lstDocProperty.Add(docProperty);
                }
                var jsonData = new { total = dsPromotion.Tables[0].Rows.Count, lstCustomerProperty = lstDocProperty };
                return new Response(lstDocProperty, dsPromotion.Tables[0].Rows.Count);
            }
            return new Response();
        }
        [WebMethod]
        [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
        public static void UpdateDocPropertyLinks(string oDocPropertyLink, bool isInsert)
        {
            List<Model.DocPropertyLink> itemPropertyLinks = JsonConvert.DeserializeObject<List<Model.DocPropertyLink>>(oDocPropertyLink);

            Promotion pmt = new Promotion();
            foreach (Model.DocPropertyLink docPropertyLink in itemPropertyLinks)
            {
                string errMsg = pmt.UpdateJsonDocPropertyLink(docPropertyLink.DocEntry, docPropertyLink.PropertyDetailID, docPropertyLink.PropertyLinkID,
                      ConfigurationManager.AppSettings["DATABASE_NAME"].ToString(), isInsert);
                if (errMsg.Length == 0)
                {
                    //Show error message
                }
            }
        }
    }
}
